% cascadeFineTuning
clear; clc;
global DeployH5_LogFile 
global bForceUpdate
bForceUpdate = true;
% bForceUpdate = false;

ExpPre  = 'warmStarting';
ResultPath = '/home/wks/SR-Works/warmStarting';

DeployH5_LogFile = fullfile(ResultPath, 'DeployH5_LogFile.log');

%%
CmdPath = fullfile(ResultPath, 'TrainingCmd23');
if ~isdir(CmdPath), mkdir(CmdPath); end
delete(fullfile(CmdPath, '*.sh'));
hCmdFile = @(order) fullfile(CmdPath, ['task' num2str(order) '.sh']);

% CmdFile = fullfile(ResultPath, 'warmStartingTraining.sh');

%%
cmd.caffe = mycaffe.caffe_cmd('c1');
cmd.solver_mode = 'gpu 1';

%% set solver param
% solv.clip_gradients = .1;
solv.clip_gradients = .01; % 1
solv.display = 100;
solv.snapshot = 100;

solv.max_iter = 6000;

solv.base_lr = .1;
solv.lr_policy = 'step' ;% 'fixed'
solv.gamma = 0.1;
solv.stepsize = 10000;

solv.momentum = 0.9;
solv.weight_decay = 0.0001;  % 0.1

%% set net param
way = 'y';
net.kernel_size = 3;
net.weight_init_type = 'msra';

%% set train & test
Train.batch_size = 64;

Patch.x_size = 41;
Patch.y_size = 41;
Patch.stride = 14;
Patch.batchsize = 64;

Aug.angles = 4; % 1 : 4;
Aug.scales = .6; % .6 : .1 : .7;  % 0.9;
% Aug.flip_dims = [1 2];
Aug.flip_dims = [];

SCALES = [3 2]; % [2 3 4];

data_startpoints = [500 1000]; % [500, 1000, 1500];
% param_startpoints = {'MSRA'};
param_startpoints = {'MSRA', 500, 1000, 1500};
% param_startpoints = {1500 3000 6000};

mynets = cell(length(SCALES), length(data_startpoints), length(param_startpoints));
mynet0List = cell(length(SCALES), 1);

% base_lr = [.1 .05 .01 .1];

bMandatoryUpdate = false;
forward_batch_size = 200;

task_cnt = 0;
for si = 1 : length(SCALES)
    scale = SCALES(si);
    
    %%
    net.result_path = fullfile(ResultPath, ['Scale', num2str(scale)]);
    net = VDSR.getNet(way, net);
    
    Patch.scales = scale;
    Train_files = srdata.getH5File('data291', Patch, Aug);
    
    %%
    Train.files = Train_files;
    cmd.weights = '';
    [mynet0, TrainingCmd] = mycaffe.genTrainingCmd(net, solv, cmd, Train, []);
    mynet0List{si} = mynet0;
    % mycaffe.logFile(hCmdFile(0), strrep(strrep(TrainingCmd, '\', '\\'), '%', '%%'), 'a', true);
    
    task_cnt = task_cnt+1;
    hWeights = @(n) mycaffe.cmd.setWeights(fullfile(mynet0.model_path, sprintf('%s%d', mynet0.model_prefix, n)));
    for di = 1 : length(data_startpoints)
        weight_file = hWeights(data_startpoints(di));
        Train.files = srdata.getDeployedTrainTest(ExpPre, Train_files, [], cmd.solver_mode, mynet0.deploy_file, weight_file, bMandatoryUpdate, forward_batch_size);
        
        for pi = 1 : length(param_startpoints)
            cmd.snapshot = '';  cmd.weights = '';
            if ~strcmpi(param_startpoints{pi}, net.weight_init_type)
                cmd.weights = hWeights(param_startpoints{pi});
            end
            [mynets{si, di, pi}, TrainingCmd] = mycaffe.genTrainingCmd(net, solv, cmd, Train, [], ExpPre);
            % system(TrainingCmd);
            mycaffe.logFile(hCmdFile(task_cnt), strrep(strrep(TrainingCmd, '\', '\\'), '%', '%%'), 'a', true);
        end
    end
end
return

%%
ImageQualities = cell(length(SCALES), length(data_startpoints), length(param_startpoints), 3);
for si = 1 : length(SCALES)
    scale = SCALES(si);
    
    ImgPairs = srimg.genLHPairs('Set5', scale);
    ImgPairs = ImgPairs(:, 2:3);
    
    for di = 1 : length(data_startpoints)
        ImgPairs1 = srdata.deployImgsByIterModels(ImgPairs, scale, mynet0List{si}, data_startpoints(di));
        for pi = 1 : length(param_startpoints)
            [~, ImageQualities{si, di, pi, :}] = srdata.deployImgsByIterModels(ImgPairs1, scale, mynets{si, di, pi});
            save(fullfile(ResultPath, 'ImageQualities-WarmParam.mat'), 'ImageQualities');
        end
    end
end

return

%%
% load(fullfile(srpath.getMatPath, 'psnrList-11.mat'));
% meanpsnrList = cellfun(@mean, psnrList(1, :), 'uniformoutput', false);
% for i = 1 : length(meanpsnrList)
%     fprintf('%d  ', psnrList{2, i});
%     fprintf('\n')
%     fprintf('%.2f ', meanpsnrList{i});
%     fprintf('\n======================\n')
% end

% return

%%
PATH = 'E:\Academic\X\!SR\2017-01-15-ResSR\paper_code';
close all; load(fullfile(PATH, 'ImageQualities-MSRA.mat')); legendstr = {'DRLN', 'DRLN-500-MSRA', 'DRLN-1000-MSRA', 'DRLN-1500-MSRA'}; LineSpecs = '--';
% load((fullfile(PATH, 'ImageQualities.mat'))); legendstr = [legendstr, 'DRLN', 'DRLN-500', 'DRLN-1000', 'DRLN-1500'];  LineSpecs = '-';
step = 1;
% LineSpecs = '--s';

k = 1; y_label = 'PSNR';
% k = 2; y_label = 'SSIM';
% ImageQualities = cell([2, size(psnrList)]);
for si = 1 : size(ImageQualities, 1)
    figure(si)
    for i = 1 : size(ImageQualities, 3)
        psnr_bar = mean([ImageQualities{si, k, i}]);
        psnr_bar = psnr_bar(1 : step : end);
        % if i == 1, psnr_bar(1) = []; end
        if i == 1
            plot(1 : length(psnr_bar), psnr_bar, LineSpecs, 'LineWidth', 1.5); hold on
        else
            plot(1 : length(psnr_bar), psnr_bar, LineSpecs);
        end
    end
    % set(gca, 'xtick', 1 : 15)
    axis tight
    xlabel('Iteration'); ylabel(y_label);
    h = legend(legendstr);
    h.Interpreter = 'none';
    h.Location = 'southeast';
    % cellfun(@(x)max(mean(x)), ImageQualities(si, k, :), 'uniformoutput', false)
end

%%
% figure;

% MatFiles = {
%     1, 'ImageQuality-Set5-scale2-Bicubic-MSRA'
%     2, 'ImageQuality-Set5-scale3-Bicubic-MSRA'
%     3, 'ImageQuality-Set5-scale4-Bicubic-MSRA'
%     3, 'ImageQuality-Set5-scale4-500-MSRA'
%     3, 'ImageQuality-Set5-scale4-500-500'
%     3, 'ImageQuality-Set5-scale4-500-1000'
%     3, 'ImageQuality-Set5-scale4-500-1500'
%     };

DataPath = 'E:\Academic\X\!SR\2017-01-15-ResSR\paper_code\500-1000-1500\scale4-clip0.1';
MatFiles = {
    'ImageQuality-Set5-scale4-Bicubic-MSRA'
    'ImageQuality-Set5-scale4-500-MSRA'
    'ImageQuality-Set5-scale4-500-500'
    'ImageQuality-Set5-scale4-500-1000'
    'ImageQuality-Set5-scale4-500-1500'
%     'ImageQuality-Set5-scale3-1000-MSRA'
%     'ImageQuality-Set5-scale3-1000-500'
%     'ImageQuality-Set5-scale3-1000-1000'
%     'ImageQuality-Set5-scale3-1000-1500'
    };

% DataPath = 'E:\Academic\X\!SR\2017-01-15-ResSR\paper_code\500-1000-1500\scale3-clip0.1';
% 
% MatFiles = {
%     'ImageQuality-Set5-scale3-Bicubic-MSRA'
%     'ImageQuality-Set5-scale3-500-MSRA'
%     'ImageQuality-Set5-scale3-500-500'
%     'ImageQuality-Set5-scale3-500-1000'
%     'ImageQuality-Set5-scale3-500-1500'
% %     'ImageQuality-Set5-scale3-1000-MSRA'
% %     'ImageQuality-Set5-scale3-1000-500'
% %     'ImageQuality-Set5-scale3-1000-1000'
% %     'ImageQuality-Set5-scale3-1000-1500'
%     };

figure;
for i = 1 : size(MatFiles, 1)
    % figure(MatFiles{i, 1})
    load(fullfile(DataPath, [MatFiles{i} '.mat']));
    plot(mean(ImageQuality{1})); hold on;
end

h = legend(MatFiles);
h.Location = 'southeast';
axis tight
set(gca, 'xlim', [0 60], 'xtick', 5 : 5 : 60);
grid on

%%
Scales = [2 3 4];
load('QualityList-MSRA-Scale234.mat')
for si = 1 : length(Scales)
    figure(si); plot(mean(QualityList{si, 1}), 'r-');
end

%%
scale = 4;
load(sprintf('QualityList-MSRA-Scale%d.mat', scale))
step = 1;
a = mean(QualityList{1, 1});
figure(scale-1); plot(a(1:step:end), 'r--');
return

%%
figure;
% load(fullfile(srpath.getMatPath, 'psnrList-11.mat'));
% pp = 0;
for i = 1 : size(psnrList, 2)-1
    %     pp = pp(end) + 1 : pp(end) + size(psnrList{1, i}, 2);
    %     if i == 1
    psnr_bar = mean([psnrList{1, [1, end]}]);
    plot(1 : length(psnr_bar), psnr_bar, '--s'); hold on
    %     else
    %         psnr_bar = mean(psnrList{1, i});
    %         plot(pp, psnr_bar, '--s');
    %     end
end
set(gca, 'xtick', 1 : 15)
axis tight
xlabel('Iteration'); ylabel('PSNR');

cellfun(@(x)max(mean(x)), psnrList(1, :), 'uniformoutput', false)

%%
load(fullfile(srpath.getMatPath, 'psnrList-5.mat'));
plot(6:10, mean([psnrList{1}]), '--o');
